package cn.hill4j.tool.spring.ext.iop.feign;

import cn.hill4j.tool.spring.ext.iop.BaseIopClientBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
/**
  * ClassName: FeignIopBuilder
  * Description: FeignIop构造器
  * @author hillchen
  * Date: 2023/8/12
  * @version: 1.0
  */
public class FeignIopBuilder extends BaseIopClientBuilder implements InitializingBean, ApplicationContextAware {
    /**
     * spring 容器上下文
     */
    private ApplicationContext applicationContext;
    /**
     * feign iop 服务名称
     */
    private String name;
    /**
     * feign iop 请求地址
     */
    private String url;
    /**
     * feign iop 上下文相对地址
     */
    private String path;
    /**
     * 404错误请求编码标志
     */
    private boolean decode404;
    /**
     * 失败回调服务类
     */
    private Class<?> fallback = void.class;
    /**
     * 失败回调服务工厂类
     */
    private Class<?> fallbackFactory = void.class;

    @Override
    public <T> T buildClient() {
        IopFeignTarget iopFeignTarget = new IopFeignTarget(name,url,path);

        IopFeignClientBuilder iopFeignClientBuilder = new IopFeignClientBuilder(applicationContext,
                contextId(),decode404,fallback,fallbackFactory,getObjectType());
        return (T)iopFeignClientBuilder.getTarget(iopFeignTarget,getObjectType());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(this.name, "Name must be set");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    /**
     * @author hillchen
     * Description: 设置feign iop 服务名称
     * Date: 2023/8/12 22:51
     * @param name feign iop 服务名称
     **/
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @author hillchen
     * Description: 设置feign iop 请求地址
     * Date: 2023/8/12 22:51
     * @param url feign iop 请求地址
     **/
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @author hillchen
     * Description: 设置feign iop 上下文相对地址
     * Date: 2023/8/12 22:51
     * @param path feign iop 上下文相对地址
     **/
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * @author hillchen
     * Description: 设置404错误请求编码标志
     * Date: 2023/8/12 22:51
     * @param decode404 404错误请求编码标志
     **/
    public void setDecode404(boolean decode404) {
        this.decode404 = decode404;
    }

    /**
     * @author hillchen
     * Description: 设置失败回调服务类
     * Date: 2023/8/12 22:51
     * @param fallback 失败回调服务类
     **/
    public void setFallback(Class<?> fallback) {
        this.fallback = fallback;
    }

    /**
     * @author hillchen
     * Description: 设置失败回调服务工厂类
     * Date: 2023/8/12 22:51
     * @param fallbackFactory 失败回调服务工厂类
     **/
    public void setFallbackFactory(Class<?> fallbackFactory) {
        this.fallbackFactory = fallbackFactory;
    }

    /**
     * @author hillchen
     * Description: 获取spring 容器上下文
     * Date: 2023/8/12 22:51
     * @return ApplicationContext
     **/
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
